<?php

namespace App\Http\Controllers\admin\comment;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

use App\Models\Comment;
use App\Models\Replay;

class StoreCommentController extends Controller
{
    //
    public function index()
    {
        $list = Comment::all('id','level','content');

        foreach ($list as $v){
            $v->user = Comment::find($v->id)->user->username;
            $v->product = Comment::find($v->id)->product->name;
            $v->brand = Comment::find($v->id)->product->brand;
            $v->store = Comment::find($v->id)->product->store->name;
            $v->replay = Comment::find($v->id)->replays->count();

        }
        //$list = Comment::find(1)->replays[0];
        //dd($list);
        return view('admin.comment.storeComment',['list'=>$list]);

    }

    public function content(Request $request)
    {
        $id = $request->input('id');

        $data = Comment::find($id,['content']);

        $data->replay = Comment::find($id)->replays->count();

        return $data;
    }

    public function replay(Request $request)
    {
        $data = $request->only('comment_id','content');
        //dd(session('storelist'));

        $data['user_id'] = session('storelist')['id'];

        $replay = Replay::create($data);

        $bool = $replay->save();

        if($bool){
            return 1;
        }else{
            return 0;
        }



    }

    public function recontent(Request $request)
    {
        $id = $request->input('id');

        $data = Comment::find($id,['content']);
        $data['recontent'] = Comment::find($id)->replays[0]->content;

        return $data;



    }

}
